﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ZEntity.Data.Helper
{
    /// <summary>
    /// 转换帮助类
    /// </summary>
    public static class ConvertUtils
    {
        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            try { return Convert.ToString(obj); }
            catch { return string.Empty; }
        }

        /// <summary>
        /// 转换为Int32
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static int ToInt32(object obj)
        {
            try { return Convert.ToInt32(obj); }
            catch { return 0; }
        }

        /// <summary>
        /// 转换为Int64
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static Int64 ToInt64(object obj)
        {
            try { return Convert.ToInt64(obj); }
            catch { return 0; }
        }

        /// <summary>
        /// 转换为DateTime
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static DateTime ToDateTime(object obj)
        {
            try { return Convert.ToDateTime(obj); }
            catch { return new DateTime(1900, 1, 1); }
        }

        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static Boolean ToBoolean(object obj)
        {
            try { return Convert.ToBoolean(obj); }
            catch { return false; }
        }

        /// <summary>
        /// 转换为float
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static float ToSingle(object obj)
        {
            try { return Convert.ToSingle(obj); }
            catch { return 0.0f; }
        }

        /// <summary>
        /// 转换为Double
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static Double ToDouble(object obj)
        {
            try { return Convert.ToDouble(obj); }
            catch { return 0.0; }
        }

        /// <summary>
        /// 转换为Decimal
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static decimal ToDecimal(object obj)
        {
            try { return Convert.ToDecimal(obj); }
            catch { return 0.0m; }
        }

        /// <summary>
        /// 转换为Guid
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static Guid ToGuid(object obj)
        {
            try { return new Guid(obj.ToString()); }
            catch { return Guid.Empty; }
        }

        /// <summary>
        /// 转换为Enum
        /// </summary>
        /// <param name="obj">待转换对象</param>
        /// <returns></returns>
        public static T ToEnum<T>(object obj)
        {
            try { return (T)Enum.Parse(typeof(T), obj.ToString() == "" ? "0" : obj.ToString()); }
            catch {  return (T)Enum.Parse(typeof(T), "0"); }
        }

        /// <summary>
        /// 转换SqlDbType名称为.net数据类型名称
        /// </summary>
        /// <param name="sqlDBTypeName"></param>
        /// <returns></returns>
        public static string ConvertSqlDbTypeToEntityType(string sqlDbTypeName) {
            string rtn = string.Empty;
            switch (sqlDbTypeName) {
                case "BigInt":
                    rtn = "Int64";
                    break;
                case "Binary":
                    rtn = "List<byte>";
                    break;
                case "Bit":
                    rtn = "bool";
                    break;
                case "Char":
                    rtn = "string";
                    break;
                case "DateTime":
                    rtn = "DateTime";
                    break;
                case "Decimal":
                    rtn = "decimal";
                    break;
                case "Float":
                    rtn = "double";
                    break;
                case "Image":
                    rtn = "List<byte>";
                    break;
                case "Int":
                    rtn = "int";
                    break;
                case "Money":
                    rtn = "decimal";
                    break;
                case "NChar":
                    rtn = "string";
                    break;
                case "NText":
                    rtn = "string";
                    break;
                case "NVarChar":
                    rtn = "string";
                    break;
                case "Real":
                    rtn = "float";
                    break;
                case "UniqueIdentifier":
                    rtn = "Guid";
                    break;
                case "SmallDateTime":
                    rtn = "DateTime";
                    break;
                case "SmallInt":
                    rtn = "Int16";
                    break;
                case "SmallMoney":
                    rtn = "decimal";
                    break;
                case "Text":
                    rtn = "string";
                    break;
                case "Timestamp":
                    rtn = "List<byte>";
                    break;
                case "TinyInt":
                    rtn = "byte";
                    break;
                case "VarBinary":
                    rtn = "List<byte>";
                    break;
                case "VarChar":
                    rtn = "string";
                    break;
                case "Variant":
                    rtn = "object";
                    break;
                case "Xml":
                    rtn = "string";
                    break;
                default:
                    break;
            }
            return rtn;
        }
    }
}